package com.lry.jvm.rtda.heap;

import com.lry.jvm.rtda.JObject;
import com.lry.jvm.util.Util;

public class ArrayObject extends JObject{

    public ArrayObject(JClass jClass) {
        super(jClass);
    }

    public static byte[] bytes(Object obj){
        return (byte[]) obj;
    }

    public static short[] shorts(Object obj){
        return (short[]) obj;
    }

    public static int[] ints(Object obj){
        return (int[]) obj;
    }

    public static long[] longs(Object obj){
        return (long[]) obj;
    }

    public static char[] chars(Object obj){
        return (char[]) obj;
    }

    public static float[] floats(Object obj){
        return (float[]) obj;
    }

    public static double[] doubles(Object obj){
        return (double[]) obj;
    }

    public static JObject[] refs(Object obj){
        return (JObject[]) obj;
    }

    public static int arrayLen(Object obj){
        if(obj instanceof byte[]){
            return bytes(obj).length;
        }else if(obj instanceof short[]){
            return shorts(obj).length;
        }else if(obj instanceof int[]){
            return ints(obj).length;
        }else if(obj instanceof long[]){
            return longs(obj).length;
        }else if(obj instanceof char[]){
            return chars(obj).length;
        }else if(obj instanceof float[]){
            return floats(obj).length;
        }else if(obj instanceof double[]){
            return doubles(obj).length;
        }else if(obj instanceof JObject[]){
            return refs(obj).length;
        }else {
            Util.panic("Not array!");
            return 0;
        }
    }

}
